from flet_core import TextStyle
import flet as ft
import win32gui

from views.common import Page,show_snack_bar,mycmd
from utils.config_file import config_obj
from utils.log import logger
from utils.config import config

def settings_view(page: Page):
    #初始化
    # config.set_debug_mode_Flag = config.debug_mode_Flag
    # config.set_enable_log_Flag = config.enable_log_Flag
    # config.set_dark_mode_Flag = config.dark_mode_Flag
    config.set_hour = config.hour
    config.set_minute = config.minute
    config.set_msg = config.msg

    def go_home(e):
        page.go("/")

    def save(e):
        go_home(e)
        # config.debug_mode_Flag = config.set_debug_mode_Flag
        # config.enable_log_Flag = config.set_enable_log_Flag
        # config.dark_mode_Flag = config.set_dark_mode_Flag
        config.hour = config.set_hour
        config.minute = config.set_minute
        config.msg = config.set_msg

        if config.dark_mode_Flag:
            page.theme_mode = ft.ThemeMode.DARK
        else:
            page.theme_mode = ft.ThemeMode.LIGHT

        if config.debug_mode_Flag:
            win32gui.ShowWindow(mycmd, 1)
        else:
            win32gui.ShowWindow(mycmd, 0)

        config_obj.config_text['debug_Mode_Enable'] = config.debug_mode_Flag
        config_obj.config_text['log_Enable'] = config.enable_log_Flag
        config_obj.config_text['dark_Mode_Enable'] = config.dark_mode_Flag
        config_obj.config_text['Time_H'] = int(config.hour)
        config_obj.config_text['Time_M'] = int(config.minute)
        config_obj.config_text['Time_MSG'] = config.msg
        config_obj.write(config_obj.config_text)

        show_snack_bar(page, "保存成功", "green")

    def debug_mode(e):
        config.debug_mode_Flag = not config.debug_mode_Flag
        if config.debug_mode_Flag:
            win32gui.ShowWindow(mycmd, 1)
        else:
            win32gui.ShowWindow(mycmd, 0)

        config_obj.config_text['debug_Mode_Enable'] = config.debug_mode_Flag
        config_obj.config_text['log_Enable'] = config.enable_log_Flag
        config_obj.config_text['dark_Mode_Enable'] = config.dark_mode_Flag
        config_obj.config_text['Time_H'] = int(config.hour)
        config_obj.config_text['Time_M'] = int(config.minute)
        config_obj.config_text['Time_MSG'] = config.msg
        config_obj.write(config_obj.config_text)

        if config.enable_log_Flag is True:
            logger.info(f"Debug模式已设置为：{config.debug_mode_Flag}")

    def enable_log(e):
        config.enable_log_Flag = not config.enable_log_Flag

        config_obj.config_text['debug_Mode_Enable'] = config.debug_mode_Flag
        config_obj.config_text['log_Enable'] = config.enable_log_Flag
        config_obj.config_text['dark_Mode_Enable'] = config.dark_mode_Flag
        config_obj.config_text['Time_H'] = int(config.hour)
        config_obj.config_text['Time_M'] = int(config.minute)
        config_obj.config_text['Time_MSG'] = config.msg
        config_obj.write(config_obj.config_text)

        if config.enable_log_Flag is True:
            logger.info(f"Log状态已设置为：{config.enable_log_Flag}")

    def dark_mode(e):
        config.dark_mode_Flag = not config.dark_mode_Flag
        if config.dark_mode_Flag:
            page.theme_mode = ft.ThemeMode.DARK
        else:
            page.theme_mode = ft.ThemeMode.LIGHT

        page.update()

        config_obj.config_text['debug_Mode_Enable'] = config.debug_mode_Flag
        config_obj.config_text['log_Enable'] = config.enable_log_Flag
        config_obj.config_text['dark_Mode_Enable'] = config.dark_mode_Flag
        config_obj.config_text['Time_H'] = int(config.hour)
        config_obj.config_text['Time_M'] = int(config.minute)
        config_obj.config_text['Time_MSG'] = config.msg
        config_obj.write(config_obj.config_text)

        if config.enable_log_Flag is True:
            logger.info(f"深色模式已设置为：{config.dark_mode_Flag}")

    def hour_changed(e):
        config.set_hour = e.data
        if config.enable_log_Flag is True:
            logger.info(f"小时已设置为：{config.set_hour}")

    def minute_changed(e):
        config.set_minute = e.data
        if config.enable_log_Flag is True:
            logger.info(f"分钟已设置为：{config.set_minute}")

    def msg_changed(e):
        config.set_msg = e.control.value
        logger.info(f"下班提示信息已设置为：{config.set_msg}")

    page.views.append(
        ft.View(
            "/",
            [
                ft.Container(
                    content=ft.Row(
                        [
                            ft.Text(
                                value="设置",
                                size=30,
                            ),
                            ft.Row(
                                [
                                    ft.ElevatedButton(
                                        "返回主页",
                                        icon="home",
                                        on_click=go_home,
                                    ),
                                    ft.ElevatedButton(
                                        "保存",
                                        icon="save",
                                        on_click=save,
                                    ),
                                ],
                                alignment="center",
                            ),
                        ],
                        alignment="spaceBetween",
                    ),
                ),
                ft.Divider(
                    height=1,
                ),
                ft.Row(
                    [
                        ft.Column(
                            [
                                ft.Column(
                                    [
                                        ft.Container(height=8),
                                        ft.Text(
                                                value="基本设置",
                                                size=30,
                                            ),
                                        ft.Switch(
                                            label="调试模式",
                                            value=config.debug_mode_Flag,
                                            on_change=debug_mode,
                                        ),
                                        ft.Switch(
                                            "打开log输出",
                                            value=config.enable_log_Flag,
                                            on_change=enable_log,
                                        ),
                                        ft.Switch(
                                            "深色模式",
                                            value=config.dark_mode_Flag,
                                            on_change=dark_mode,
                                        ),
                                    ],
                                ),
                                ft.Container(height=20),
                                ft.Text(
                                    value="设置下班时间",
                                    size=30,
                                ),
                                ft.Row(
                                    [
                                        ft.Dropdown(
                                            width=100,
                                            label="小时",
                                            hint_text="选择小时",
                                            options=[
                                                ft.dropdown.Option(str(i))
                                                for i in range(0, 24)
                                            ],
                                            text_style=TextStyle(color=page.theme.color_scheme.primary, weight=ft.FontWeight.W_600),
                                            value=config.hour,
                                            on_change=hour_changed,
                                        ),
                                        ft.Text(
                                            value=":",
                                            size=30,
                                        ),
                                        ft.Dropdown(
                                            width=100,
                                            label="分钟",
                                            hint_text="选择分钟",
                                            options=[
                                                ft.dropdown.Option(str(i))
                                                for i in range(0, 60)
                                            ],
                                            text_style=TextStyle(color=page.theme.color_scheme.primary, weight=ft.FontWeight.W_600),
                                            value=config.minute,
                                            on_change=minute_changed,
                                        ),
                                    ],
                                ),
                                ft.Container(height=20),
                                ft.Text(
                                    value="设置下班提示信息",
                                    size=30,
                                ),
                                ft.TextField(
                                    label="下班提示信息:",
                                    hint_text="请输入下班提示信息",
                                    value = config.msg,
                                    on_change=msg_changed,
                                ),
                            ],
                            alignment="spaceAround",
                            horizontal_alignment="center",
                        ),
                    ],
                    alignment="center",
                ),


            ],
            bgcolor=page.theme.color_scheme.background,
        )
    )
